﻿Imports System.IO
Imports System.Text

Public Class AWSMapSerializer
    Implements ISerializer(Of AWSMap)

    Public Function Desrialize(ByVal stream As Stream) As AWSMap Implements ISerializer(Of AWSMap).Desrialize
        Dim map As New AWSMap

        Dim buffer(4999) As Byte
        stream.Read(buffer, 0, 10)
        buffer(10) = 0
        map.TypeName = Encoding.UTF8.GetString(buffer)

        If map.TypeName.StartsWith("AWSMap001") Then
            stream.Read(buffer, 0, 2)
            map.SetSize(buffer(0), buffer(1))
        End If

        Select Case map.TypeName
            Case "AWDMap001"
                stream.Read(buffer, 0, 1)
                map.Type = buffer(0) - 1
            Case "AWSMap001"
                stream.Read(buffer, 0, 1)
                map.Type = buffer(0)
        End Select

        '读取地形
        For i = 0 To map.Width - 1
            For j = 0 To map.Height - 1
                stream.Read(buffer, 0, 2)
                map.ImgByte(i, j) = buffer(1) * 256 + buffer(0)
                map.t1(i, j) = buffer(0)
                map.t2(i, j) = buffer(1)
            Next
        Next

        '读取单位
        For i = 0 To map.Width - 1
            For j = 0 To map.Height - 1
                stream.Read(buffer, 0, 2)
                map.UnitByte(i, j) = buffer(1) * 256 + buffer(0)
                map.u1(i, j) = buffer(0)
                map.u2(i, j) = buffer(1)
            Next
        Next

        If stream.ReadByte <> -1 Then
            '读取地图名称
            Dim fieldsize As ULong = 0
            stream.Read(buffer, 0, 4)
            For i = 0 To 3
                fieldsize += buffer(i) * (256 ^ i)
            Next
            If fieldsize Then
                stream.Read(buffer, 0, fieldsize)
                map.Name = Encoding.UTF8.GetString(buffer)
            End If

            '读取地图作者
            fieldsize = 0
            stream.Read(buffer, 0, 4)
            For i = 0 To 3
                fieldsize += buffer(i) * (256 ^ i)
            Next
            If fieldsize Then
                stream.Read(buffer, 0, fieldsize)
                map.Author = Encoding.UTF8.GetString(buffer)
            End If

            '读取地图描述
            fieldsize = 0
            stream.Read(buffer, 0, 4)
            For i = 0 To 3
                fieldsize += buffer(i) * (256 ^ i)
            Next
            If fieldsize Then
                stream.Read(buffer, 0, fieldsize)
                map.Description = Encoding.UTF8.GetString(buffer)
            End If
        End If

        Return map
    End Function

    Public Sub Serialize(ByRef stream As Stream, ByVal map As AWSMap) Implements ISerializer(Of AWSMap).Serialize
        Dim buffer(4999) As Byte

        Dim b() As Byte = Encoding.UTF8.GetBytes(map.TypeName)
        stream.Write(b, 9, b.Length)

        buffer(0) = 0
        stream.Write(buffer, 1, buffer.Length)
    End Sub

End Class
